package cn.daichi.rabbitdemo.ttl.queuettl;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class QueueTtlConfig {

    public static final String QUEUE_TTL_QUEUE = "queue_ttl_queue";
    public static final String QUEUE_TTL_EXCHANGE = "queue_ttl_exchange";
    public static final String QUEUE_TTL_ROUTING_KEY = "queue_ttl_routing_key";

    @Bean
    Queue queueTtlQueue() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-message-ttl", 10000);
        return new Queue(QUEUE_TTL_QUEUE, true, false, false, args);
    }

    @Bean
    DirectExchange queueTtlDirectExchange() {
        return new DirectExchange(QUEUE_TTL_EXCHANGE, true, false);
    }

    @Bean
    Binding queueTtlBinding() {
        return BindingBuilder.bind(queueTtlQueue())
                .to(queueTtlDirectExchange())
                .with(QUEUE_TTL_ROUTING_KEY);
    }

}
